﻿Option Explicit On
Option Strict On


''' <summary>
''' Objecto para jestionar una agenda de nota
''' </summary>
''' <remarks>Francisco Juan Royo Bua</remarks>
Public Class ObjCalendario

#Region "atributos"

    Private _labelHoras As Label
    Private _labelLunes As Label
    Private _labelMartes As Label
    Private _labelMiercoles As Label
    Private _labelJueves As Label
    Private _labelViernes As Label
    Private _labelSabado As Label
    Private _labelDomingo As Label
    Private _DataGridCalendario As DataGridView
    Private _Panel As Panel

    'atributos de tiempo
    Private _IntervaloTiempo As Integer
    Private _diaSemana As _diasSemana
    Public Enum _diasSemana As Integer
        Lunes = 1
        Martes = 2
        Miercoles = 3
        Jueves = 4
        Viernes = 5
        Sabado = 6
        Domingo = 7
    End Enum

    'boolean
    Private _AutoAjusteDataGrid As Boolean

    'tamaño y posición
    Private _ancho As Integer
    Private _alto As Integer
    Private _x As Integer
    Private _y As Integer
    Private _Empleados As Integer

    'colores tarde y mañana
    Private _ColorManyana As Color
    Private _ColorTarde As Color


#End Region

#Region "Contructores y destructores"


    Public Sub New(ByVal control As Control)

        _ancho = 200
        _alto = 200
        _x = 0
        _y = 0
        _Empleados = 1
        _ColorManyana = Color.Gold
        _ColorTarde = Color.Gray
        _IntervaloTiempo = 15
        _diaSemana = _diasSemana.Domingo

        Me.CrearPanel(control)
        Me.CrearControlesLabel(_Panel)
        Me.CrearControlDataGriew(_Panel)
        Me._AutoAjusteDataGrid = True 'ajustara las demensiones del data grid automaticamente

        Me.AutoRedimensionDataGrid()
    End Sub


    Public Sub New(ByVal control As Control, ByVal Empleados As Integer)

        _ancho = 200
        _alto = 200
        _x = 0
        _y = 0
        _Empleados = Empleados
        _ColorManyana = Color.Gold
        _ColorTarde = Color.Gray
        _IntervaloTiempo = 15
        _diaSemana = _diasSemana.Domingo

        Me.CrearPanel(control)
        Me.CrearControlesLabel(_Panel)
        Me.CrearControlDataGriew(_Panel)
        Me._AutoAjusteDataGrid = True 'ajustara las demensiones del data grid automaticamente

        Me.AutoRedimensionDataGrid()
    End Sub

    Public Sub New(ByVal control As Control, ByVal Empleados As Integer, ByVal diaSemana As _diasSemana)

        _ancho = 200
        _alto = 200
        _x = 0
        _y = 0
        _Empleados = Empleados
        _ColorManyana = Color.Gold
        _ColorTarde = Color.Gray
        _IntervaloTiempo = 15
        _diaSemana = diaSemana

        Me.CrearPanel(control)
        Me.CrearControlesLabel(_Panel)
        Me.CrearControlDataGriew(_Panel)
        Me._AutoAjusteDataGrid = True 'ajustara las demensiones del data grid automaticamente

        Me.AutoRedimensionDataGrid()
    End Sub


    Public Sub New(ByVal control As Control, ByVal ColorManyna As Color, ByVal ColorTarde As Color, ByVal Empleados As Integer, ByVal DiasSemana As _diasSemana)

        _ancho = 200
        _alto = 200
        _x = 0
        _y = 0
        _Empleados = Empleados
        _ColorManyana = ColorManyna
        _ColorTarde = ColorTarde
        _IntervaloTiempo = 15
        _diaSemana = DiasSemana

        Me.CrearPanel(control)
        Me.CrearControlesLabel(_Panel)
        Me.CrearControlDataGriew(_Panel)
        Me._AutoAjusteDataGrid = True 'ajustara las demensiones del data grid automaticamente

        Me.AutoRedimensionDataGrid()
    End Sub




    Public Sub New(ByVal control As Control, ByVal Ancho As Integer, ByVal Alto As Integer, ByVal posX As Integer, ByVal posY As Integer, ByVal ColorManyna As Color, ByVal ColorTarde As Color, ByVal Empleados As Integer, ByVal DiasSemana As _diasSemana)

        _ancho = Ancho
        _alto = Alto
        _x = posX
        _y = posY
        _Empleados = Empleados
        _ColorManyana = ColorManyna
        _ColorTarde = ColorTarde
        _IntervaloTiempo = 15
        _diaSemana = DiasSemana

        Me.CrearPanel(control)
        Me.CrearControlesLabel(_Panel)
        Me.CrearControlDataGriew(_Panel)
        Me._AutoAjusteDataGrid = True 'ajustara las demensiones del data grid automaticamente

        Me.AutoRedimensionDataGrid()
    End Sub

#End Region

#Region "get y set"

    Public Property labelUno As label
        Get
            Return _labelLunes
        End Get
    End Property

    Public Property DiasSemana As _diasSemana
        Get
            Return _diaSemana
        End Get
        Set(ByVal value As _diasSemana)
            _diaSemana = value
        End Set
    End Property

    Public ReadOnly Property IntervaloTiempo As Integer
        Get
            Return _IntervaloTiempo
        End Get

    End Property

    Public Property ColorMañana() As Color
        Get
            Return _ColorManyana
        End Get
        Set(ByVal value As Color)
            _ColorManyana = value
        End Set
    End Property


    Public Property ColorTarde() As Color
        Get
            Return _ColorTarde
        End Get
        Set(ByVal value As Color)
            _ColorTarde = value
        End Set
    End Property



    Public Property DataGriewCalendario() As DataGridView
        Get
            Return _DataGridCalendario
        End Get
        Set(ByVal value As DataGridView)
            _DataGridCalendario = value
        End Set
    End Property

    Public Property AutoAjuste() As Boolean
        Get
            Return _AutoAjusteDataGrid
        End Get
        Set(ByVal value As Boolean)
            _AutoAjusteDataGrid = value
        End Set
    End Property

    'Public Property Panel() As Panel
    '    Get
    '        Return _Panel
    '    End Get
    '    'Set(ByVal value As Panel)

    '    'End Set
    '    Set(ByVal value As Panel)

    '    End Set
    'End Property

    Public Property Ancho As Integer
        Get
            Return _ancho
        End Get
        Set(ByVal value As Integer)
            _ancho = value
            _Panel.Width = _ancho
        End Set
    End Property

    Public Property Alto As Integer
        Get
            Return _alto
        End Get
        Set(ByVal value As Integer)
            _alto = value
            _Panel.Height = _alto
        End Set
    End Property

    Public Property X As Integer
        Get
            Return _x
        End Get
        Set(ByVal value As Integer)
            _x = value
            _Panel.Location = New Point(value, _y)
        End Set
    End Property

    Public Property Y As Integer
        Get
            Return _y
        End Get
        Set(ByVal value As Integer)
            _y = value
            _Panel.Location = New Point(_x, value)
        End Set
    End Property

    Public ReadOnly Property Empleado As Integer
        Get
            Return _Empleados
        End Get

    End Property

#End Region


#Region "Metodos publicos"

    Public Function AnchoTotal() As Integer

        Return _labelHoras.Width + (_labelLunes.Width * 7)

    End Function

    Public Sub DibujarCitas(ByVal DiasSemana As Integer, ByVal HoraInicio As String, ByVal CodEmpleado As Integer, ByVal UndTiempo As Integer)
        Dim valorDiaSemana As Integer = 0
        Dim valorHora As Integer = 0
        'dar un valor al dia semana
        valorDiaSemana = DiasSemana
        'recorre todo el calendario en busca del valor de la hora en el índice
        For indiceFila = 0 To _DataGridCalendario.Rows.Count - 1
            If _DataGridCalendario.Rows(indiceFila).Cells(0).Value.ToString = HoraInicio Then

                valorHora = indiceFila
                indiceFila = _DataGridCalendario.Rows.Count + 10
            End If
        Next

        'ahora averiguar la celda a pintar atrás vez del empleado y día semana
        Dim indiceCelda As Integer = (CodEmpleado + 1) + (valorDiaSemana * _Empleados)

        'ahora pintamos
        Dim auxValorHora As Integer = 0
        For indiceFila = 0 To UndTiempo
            auxValorHora = valorHora + indiceFila
            _DataGridCalendario.Rows(auxValorHora).Cells(indiceCelda).Style.BackColor = Color.Red
        Next
    End Sub

    ''' <summary>
    ''' Esta blece el rango de Horas de la agenda
    ''' </summary>
    ''' <param name="horaIncial">String del siguiente formato "00:00" por donde empieza</param>
    ''' <param name="horaFinal">String del siguiente formato "00:00" por donde termina</param>
    ''' <remarks>Francisco Juan Royo Bua - 21/01/2012</remarks>
    Public Sub EstaBlecerRangoHoras(ByVal horaIncial As String, ByVal horaFinal As String)

        Dim hora As Integer = CInt(horaIncial.Substring(0, 2))
        Dim horaF As Integer = CInt(horaFinal.Substring(0, 2))
        Dim valorComparar As Integer

        'primero el intervalo de horas 
        Dim ultimoIndice As Integer = -1
        For indiceFilas = 0 To _DataGridCalendario.Rows.Count - 1
            Try
                valorComparar = CInt(_DataGridCalendario.Rows(indiceFilas).Cells(0).Value.ToString.Substring(0, 2))
                If valorComparar >= hora And valorComparar <= horaF Then
                    ultimoIndice = indiceFilas
                Else
                    _DataGridCalendario.Rows.RemoveAt(indiceFilas)
                    If indiceFilas = 0 Then
                        indiceFilas = ultimoIndice
                    Else
                        indiceFilas = ultimoIndice - 1
                    End If
                    'Else
                    '    _DataGridCalendario.Rows(x).Visible = False

                End If
            Catch

            End Try

        Next

        'ahora eliminamos los primeros que no cuadre con la hora
        Try
            For indiceFilas = 0 To _DataGridCalendario.Rows.Count - 1
                If horaIncial = _DataGridCalendario.Rows(indiceFilas).Cells(0).Value.ToString Then
                    indiceFilas = _DataGridCalendario.Rows.Count + 10
                Else
                    _DataGridCalendario.Rows.RemoveAt(indiceFilas)
                    indiceFilas = -1
                    'Else
                    '    _DataGridCalendario.Rows(x).Visible = False

                End If
            Next
        Catch

        End Try
       


        'ahora eliminamos los primeros que no cuadre con la hora
        Try
            For indiceFilas = _DataGridCalendario.Rows.Count - 1 To 0 Step -1
                If horaFinal = _DataGridCalendario.Rows(indiceFilas).Cells(0).Value.ToString Then
                    indiceFilas = -1
                Else
                    _DataGridCalendario.Rows.RemoveAt(indiceFilas)
                    indiceFilas = _DataGridCalendario.Rows.Count
                    'Else
                    '    _DataGridCalendario.Rows(x).Visible = False

                End If
            Next
        Catch

        End Try
        

        Me.AutoRedimensionDataGrid()

    End Sub

#End Region

#Region "Metodos privados"

    Private Sub CrearPanel(ByVal control As Control)

        _Panel = New Panel
        With _Panel
            .Name = "Panel1"
            .Width = _ancho
            .Height = _alto
            .Location = New Point(_x, _y)
            .AutoScroll = True
            .AutoSize = False
        End With

        control.Controls.Add(_Panel)

    End Sub


    Private Sub CrearControlesLabel(ByVal control As Control)
        Dim anchoLabel As Integer = 100
        Dim altoLabel As Integer = 20
        Dim xLabel As Integer = 0
        Dim yLabel As Integer = 0

        'Dependiendo la cantida de empleados cambia el tamaño del ancho
        If _Empleados > 2 Then
            anchoLabel = 25 * _Empleados
        End If

        _labelHoras = New Label()
        With _labelHoras
            .Name = "labelHoras"
            .Text = "Horas"
            .Width = 100
            .Height = altoLabel
            .Location = New Point(xLabel, yLabel)
            .Visible = True
            .BackColor = Color.Aquamarine
            .TextAlign = ContentAlignment.MiddleCenter
        End With
        control.Controls.Add(_labelHoras)

        xLabel = 100 - anchoLabel

        If _diasSemana.Lunes <= _diaSemana Then
            _labelLunes = New Label()
            With _labelLunes
                .Name = "labelLunes"
                .Text = "Lunes"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + anchoLabel, yLabel)
                .Visible = True
                .BackColor = Color.Coral
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelLunes)

        End If


        If _diasSemana.Martes <= _diaSemana Then

            _labelMartes = New Label()
            With _labelMartes
                .Name = "labeMartes"
                .Text = "Martes"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + (anchoLabel * 2), yLabel)
                .Visible = True
                .BackColor = Color.CadetBlue
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelMartes)
        End If

        If _diasSemana.Miercoles <= _diaSemana Then
            _labelMiercoles = New Label()
            With _labelMiercoles
                .Name = "labelMiercoles"
                .Text = "Miercoles"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + (anchoLabel * 3), yLabel)
                .Visible = True
                .BackColor = Color.YellowGreen
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelMiercoles)

        End If

        If _diasSemana.Jueves <= _diaSemana Then

            _labelJueves = New Label()
            With _labelJueves
                .Name = "labelJueves"
                .Text = "Jueves"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + (anchoLabel * 4), yLabel)
                .Visible = True
                .BackColor = Color.Yellow
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelJueves)
        End If

        If _diasSemana.Viernes <= _diaSemana Then

            _labelViernes = New Label()
            With _labelViernes
                .Name = "labelViernes"
                .Text = "Viernes"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + (anchoLabel * 5), yLabel)
                .Visible = True
                .BackColor = Color.Orange
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelViernes)
        End If


        If _diasSemana.Sabado <= _diaSemana Then
            _labelSabado = New Label()
            With _labelSabado
                .Name = "labelSabado"
                .Text = "Sabado"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + (anchoLabel * 6), yLabel)
                .Visible = True
                .BackColor = Color.Pink
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelSabado)
        End If

        If _diasSemana.Domingo <= _diaSemana Then

            _labelDomingo = New Label()
            With _labelDomingo
                .Name = "labelDomingo"
                .Text = "Domingo"
                .Width = anchoLabel
                .Height = altoLabel
                .Location = New Point(xLabel + (anchoLabel * 7), yLabel)
                .Visible = True
                .BackColor = Color.OrangeRed
                .TextAlign = ContentAlignment.MiddleCenter
            End With

            control.Controls.Add(_labelDomingo)
        End If

    End Sub

    Private Sub CrearControlDataGriew(ByVal control As Control)
        _DataGridCalendario = New DataGridView
        Dim ancho As Integer = 100
        Dim alto As Integer = 20

        'Dependiendo la cantida de empleados cambia el tamaño del ancho
        If _Empleados = 2 Then
            ancho = 50
        ElseIf _Empleados > 2 Then
            ancho = 25
        End If

        With _DataGridCalendario
            .Name = "DataGridCalendario"
            .BorderStyle = BorderStyle.None
            .ColumnHeadersVisible = False
            .RowHeadersVisible = False
            .Width = _labelHoras.Width + (_labelLunes.Width * _diaSemana)
            .Height = 25 * 96
            .Location = New Point(0, _labelLunes.Height)
            .CellBorderStyle = DataGridViewCellBorderStyle.None
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False


        End With
        'crea mas o menos dependiendo los empleados
        _DataGridCalendario.Columns.Add("Colum" & 0, "")
        _DataGridCalendario.Columns(0).Width = 100
        _DataGridCalendario.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        For indiceColumns = 1 To (_diaSemana * _Empleados)
            _DataGridCalendario.Columns.Add("Colum" & X, "")
            _DataGridCalendario.Columns(indiceColumns).Width = ancho
            _DataGridCalendario.Columns(indiceColumns).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Next

        Dim intervalofijo As Integer = _IntervaloTiempo
        Dim horas As Integer = 0
        Dim minutos As Integer = 0
        Dim intervaloVariado As String

        '_DataGridCalendario.Rows.Add()
        '_DataGridCalendario.Rows(0).Height = alto
        '_DataGridCalendario.Rows(0).SetValues("Empleado")
        '_DataGridCalendario.Rows(0).Cells(0).Style.BackColor = Color.AliceBlue

        For indiceRows = 0 To 95
            _DataGridCalendario.Rows.Add()
            _DataGridCalendario.Rows(indiceRows).Height = alto

            minutos += intervalofijo

            If indiceRows Mod 4 = 0 Then
                horas += 1
                minutos = 0
            End If

            If horas = 24 Then
                intervaloVariado = "00:" & minutos.ToString("00")
            Else
                intervaloVariado = horas.ToString("00") & ":" & minutos.ToString("00")

            End If
            _DataGridCalendario.Rows(indiceRows).SetValues(intervaloVariado)
            'Las cambiamos de color para diferenciarlas con la tarde
            If horas >= 15 Then
                _DataGridCalendario.Rows(indiceRows).Cells(0).Style.BackColor = Me._ColorTarde
            Else
                _DataGridCalendario.Rows(indiceRows).Cells(0).Style.BackColor = Me._ColorManyana
            End If
        Next

        control.Controls.Add(_DataGridCalendario)

    End Sub

    ''' <summary>
    ''' Su uso es despues de crear los controles del data grid y label
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AutoRedimensionDataGrid()

        Dim anchoData As Integer
        Dim altoData As Integer
        'Comprobar si tiene permiso
        If _AutoAjusteDataGrid Then

            anchoData = _labelHoras.Width + (_labelLunes.Width * 7)
            'calcular el alto
            If _DataGridCalendario.Rows.Count >= 1 Then
                altoData += _DataGridCalendario.Rows(0).Height * _DataGridCalendario.Rows.Count
            Else
                altoData = 0
            End If


            _DataGridCalendario.Width = anchoData
            _DataGridCalendario.Height = altoData
        End If


    End Sub

#End Region


#Region "Metodos prueba"
    Public Function RetornarCadenaHoras() As List(Of String)
        Dim cadenaHoras As New List(Of String)

        For indice = 0 To _DataGridCalendario.Rows.Count - 1
            If cadenaHoras.Contains(_DataGridCalendario.Rows(indice).Cells(0).Value.ToString.Substring(0, 2)) Then


            Else
                cadenaHoras.Add(_DataGridCalendario.Rows(indice).Cells(0).Value.ToString.Substring(0, 2))

            End If


        Next


        Return cadenaHoras
    End Function
#End Region

End Class
